//
//  TabBarController.swift
//  Base
//
//  Created by Flutter on 2021/7/8.
//

import UIKit
import Standard

open class TabBarController: UITabBarController {

    open private(set) var isFirstLoad: Bool = true

    public required init(viewControllers: [UIViewController]?, nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.viewControllers = viewControllers
    }

    @available(*, unavailable)
    public required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") }

}

// MARK: - TypeName

extension TabBarController: TypeName {}

// MARK: - Override

extension TabBarController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }

    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        logger.trace("\(typeName) viewWillAppear")
    }

    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        isFirstLoad = false
    }

    override open func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        logger.trace("\(typeName) viewWillDisappear")
    }
}


// MARK: - Private

private extension TabBarController {
    func setup() {
        view.backgroundColor = .systemBackground
    }
}

// MARK: - Override Config

public extension TabBarController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        viewControllers?[safe: selectedIndex]?.preferredStatusBarStyle ?? .default
    }

    override var shouldAutorotate: Bool {
        viewControllers?[safe: selectedIndex]?.shouldAutorotate ?? false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        viewControllers?[safe: selectedIndex]?.supportedInterfaceOrientations ?? .portrait
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        viewControllers?[safe: selectedIndex]?.preferredInterfaceOrientationForPresentation ?? .portrait
    }
}
